from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import biz_mysql
import excel_info_sql
list1 = excel_info_sql.getConditions()

def Inovel_test():

    driver = webdriver.Chrome()
    driver.get("https://www.niuniutui.com/")
    driver.maximize_window()
    driver.implicitly_wait(20)
    driver.find_element_by_xpath('//a[text()="登录"]').click()
    # 定位账号
    driver.find_element_by_xpath('//*[@id="account"]').clear()
    driver.find_element_by_xpath('//*[@id="account"]').send_keys("")
    # 定位密码
    driver.find_element_by_xpath('//*[@id="password"]').clear()
    driver.find_element_by_xpath('//*[@id="password"]').send_keys('')
    # 验证码
    capt = input(">请输入验证码:")
    driver.find_element_by_xpath('//*[@id="captcha"]').send_keys(capt)
    # 点击登录
    driver.find_element_by_xpath('//*[@id="submit_btn"]').click()

    if '情眯' in list1[0]['Platform']:
        pass
    elif '媚色' in list1[0]['Platform']:
        try:
            WebDriverWait(driver,60).until(
                EC.element_to_be_clickable((By.XPATH,'//li[2]//span'))
            )
            driver.find_element_by_xpath('//li[2]//span').click()
        except:
            pass
        try:
            driver.find_element_by_xpath('//*[@id="header"]//li[2]//dd[1]/a').click()
        except:
            pass

    elif '魔情' in list1[0]['Platform']:
        try:
            WebDriverWait(driver,60).until(
                EC.element_to_be_clickable((By.XPATH,'//li[2]//span'))
            )
            driver.find_element_by_xpath('//li[2]//span').click()
        except:
            pass
        driver.find_element_by_xpath('//*[@id="header"]//li[2]//dd[2]/a').click()
    elif '羞涩' in list1[0]['Platform']:
        try:
            WebDriverWait(driver,60).until(
                EC.element_to_be_clickable((By.XPATH,'//li[2]//span'))
            )
            driver.find_element_by_xpath('//li[2]//span').click()
        except:
            pass
        driver.find_element_by_xpath('//*[@id="header"]//li[2]//dd[3]/a').click()
    # 进入产品后台
    time.sleep(0.5)
    driver.find_element_by_xpath('//a[text()="进入产品后台"]').click()

    for i in range(0, len(list1)):
        # 小说名字
        Novel_name = list1[i]["Novel_name"]
        # 日期
        insert_time = list1[i]["date_time"]
        # 章节
        Chapter = list1[i]["Chapter"]
        # 推广名称
        Extension_name = list1[i]["Extension_name"]
        # 平台名称
        Platform = list1[i]["Platform"]
        # 小说列表
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//div/ul/li[9]/a'))
            )
            driver.find_element_by_xpath('//div/ul/li[9]/a').click()
        except Exception:
            print("点击小说列表")

        # 总付费榜
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//a[text()="总付费榜"]'))
            )
            driver.find_element_by_xpath('//a[text()="总付费榜"]').click()
        except Exception:
            print("点击总付费榜")

        # 输入关键字
        try:
            time.sleep(0.5)
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//input[2]'))
            )
            driver.find_element_by_xpath('//input[2]').clear()
            driver.find_element_by_xpath('//input[2]').send_keys(Novel_name)
        except Exception:
            print("点击输入关键字")

        # 搜索
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//button'))
            )
            driver.find_element_by_xpath('//button').click()
        except Exception:
            print("点击搜索")

        # 获取内推链接
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//td[6]/a[2]'))
            )
            driver.find_element_by_xpath('//td[6]/a[2]').click()
        except Exception:
            print("点击获取内推链接")

        # 获取第一条内推链接
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="getlinkbtn1"]'))
            )
            driver.find_element_by_xpath('//*[@id="getlinkbtn1"]').click()
        except Exception:
            print("点击获取第一条内推链接")

        # 渠道名称
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="title"]'))
            )
            driver.find_element_by_xpath('//*[@id="title"]').clear()
            driver.find_element_by_xpath('//*[@id="title"]').send_keys(str(insert_time)+str(Extension_name))
        except Exception:
            print("点击渠道名称")

        # 生成链接
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//*[@id="makelinkbtn"]'))
            )
            driver.find_element_by_xpath('//*[@id="makelinkbtn"]').click()
        except Exception:
            print("点击生成链接")

        # 获取url
        try:
            time.sleep(3)
            url = driver.execute_script( "return jQuery('[id=linkurl]').val();")
            print(url)
            # 数据入库
            try:
                biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name,Chapter=Chapter,
                                             datetime=insert_time, Extension_name=str(insert_time)+str(Extension_name))
            except Exception as e:
                print(e)
                print("数据入库失败")
        except Exception:
            print("选择url框出错")

        # 取消
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//span[@class="layui-layer-setwin"]'))
            )
            driver.find_element_by_xpath('//span[@class="layui-layer-setwin"]').click()
        except Exception:
            print("点击取消")

    driver.quit()
Inovel_test()